package com.west_traveler.ximalayafm;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class SearchActivity extends AppCompatActivity {

    private SearchView searchView;
    private Button btnCancle;
    private LinearLayout llContainer;
    private int downX;
    private int downY;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        initView();

        aboutSearchView();

        aboutSlideClose();


    }

    private void aboutSearchView() {
        searchView.setIconifiedByDefault(false);

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });

    }

    private void aboutSlideClose() {
        btnCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
                overridePendingTransition(android.R.anim.fade_in, R.anim.push_left_out);
            }
        });

        llContainer.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        downX = (int) motionEvent.getX();
                        downY = (int) motionEvent.getY();
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        if (Math.abs(downX - motionEvent.getX()) > Math.abs(downY - motionEvent.getY()) && downX - motionEvent.getX()<-10) {
                            finish();
                            overridePendingTransition(android.R.anim.fade_in, R.anim.push_left_out);
                        }
                        break;
                }


                return true;
            }
        });

    }

    private void initView() {
        llContainer = (LinearLayout) findViewById(R.id.ll_container_id);
        searchView = (SearchView) findViewById(R.id.sv_search_id);
        btnCancle = (Button) findViewById(R.id.btn_cancle_id);

    }
}
